package com.gamerole.app.page.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.gamerole.app.R
import com.gamerole.app.databinding.AppRecyItemChatBinding
import com.gamerole.app.databinding.AppRecyItemUpSearchBinding
import com.gamerole.app.entity.ArticleBean
import com.gamerole.app.entity.UpSearchBean
import com.gamerole.commom.extention.image
import com.zzhoujay.richtext.RichText

class UpSearchAdapter : PagingDataAdapter<UpSearchBean, UpSearchAdapter.ViewHolder>(COMPARATOR) {
    companion object {
        //因为Paging 3在内部会使用DiffUtil来管理数据变化，所以这个COMPARATOR是必须的
        private val COMPARATOR = object : DiffUtil.ItemCallback<UpSearchBean>() {
            override fun areItemsTheSame(oldItem: UpSearchBean, newItem: UpSearchBean): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(oldItem: UpSearchBean, newItem: UpSearchBean): Boolean {
                return oldItem == newItem
            }
        }
    }

    class ViewHolder(private val binding: AppRecyItemUpSearchBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(data: UpSearchBean) {
            binding.apply {
                 RichText.fromHtml(data.title).into(tvTitle)
                tvUpdateTime.text = data.insert_time
            }

        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val tile = getItem(position)
        if (tile != null) {
            holder.bind(tile)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view =
            AppRecyItemUpSearchBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(view)
    }
}